import 'amis/lib/themes/cxd.css'
import { CRUDTableSchema } from 'amis/lib/renderers/CRUD'
import { PaginationSchema } from 'amis/lib/renderers/Pagination'
import { ReloadActionSchema } from 'amis/lib/renderers/Action'
import { Schema } from 'amis/lib/types'
import { PanelSchema } from 'amis/lib/renderers/Panel'
import { ColumnTogglerSchema } from 'amis/lib/renderers/Table2/ColumnToggler'
import { SuperTableProps } from '@/components/amis/SuperTable/types'
import { TableColumn } from 'amis/lib/renderers/Table'

class SuperTableRender {
	readonly DEFAULT_PAGINATION: Partial<PaginationSchema> = {
		type: 'pagination',
		layout: 'perPage,pager,go',
		mode: 'normal',
		perPage: 10,
		align: 'right',
		maxButtons: 7,
		showPerPage: true,
		perPageAvailable: [10, 20, 50, 100],
		showPageInput: true,
		disabled: false,
	} as Partial<PaginationSchema>

	readonly DEFAULT_PAGINATION_STATISTICS = {
		type: 'statistics',
		align: 'left',
	}

	readonly DEFAULT_CRUD_SCHEMA: CRUDTableSchema = {
		type: 'crud',
		mode: 'table',
		autoGenerateFilter: {
			columnsNum: 5,
			showBtnToolbar: false,
			defaultCollapsed: true,
		},
		primaryField: 'id',
		keepItemSelectionOnPageChange: false,
		syncLocation: true,
		autoFillHeight: true,
		filterTogglable: false,
		perPageField: 'size',
		headerToolbar: [
			'bulkActions',
			{
				type: 'columns-toggler',
				align: 'right',
				draggable: true,
				icon: 'fas fa-cog',
				tooltip: '列设置',
			} as Partial<Omit<ColumnTogglerSchema, 'type'>>,
			{
				type: 'button',
				actionType: 'reload',
				align: 'right',
				icon: 'fa-solid fa-arrows-rotate',
				label: '',
				tooltip: '刷新',
			} as ReloadActionSchema,
		],
		footerToolbar: [this.DEFAULT_PAGINATION_STATISTICS, this.DEFAULT_PAGINATION],
		alwaysShowPagination: true,
	} as CRUDTableSchema

	readonly DEFAULT_PANEL_SCHEMA: PanelSchema = { type: 'panel' } as PanelSchema
	getSchema = (props: SuperTableProps): Schema => {
		const panelSchema = Object.assign(
			{ title: props.title ? `<b style='font-size: large;' >${props.title}</b>` : undefined } as PanelSchema,
			this.DEFAULT_PANEL_SCHEMA,
		)
		const mergeCrudSchema: CRUDTableSchema = Object.assign({}, this.DEFAULT_CRUD_SCHEMA, props.tableSchema, {
			headerToolbar: (props.tableSchema.headerToolbar ? props.tableSchema.headerToolbar : []).concat(
				this.DEFAULT_CRUD_SCHEMA.headerToolbar!,
			),
		} as CRUDTableSchema)
		panelSchema.body = mergeCrudSchema
		//const schema = { type: 'page', body: panelSchema }
		const schema = mergeCrudSchema
		return schema
	}

	// 生成时间列的schema
	buildDateTimeColumnSchema = (columnSchema: TableColumn): TableColumn => {
		const result = Object.assign(
			{
				type: 'tpl',
				tpl: `\${${columnSchema.name}|datetime}`,
			} as Partial<TableColumn>,
			columnSchema,
		)
		return result
	}
}

export default new SuperTableRender()
